<?php
/**
 * Created by PhpStorm.
 * User: yaolei
 * Date: 2017/11/12
 * Time: 21:30
 */

namespace Home\Model;

use Think\Model;

class Balance extends Model
{
    protected $tableName = 'user_balance';
    public $number;
    public $error;
    public $type=1;
    public $error_content="";
    public $content;

    protected $_validate=[
        ['number','required','请输入整数']
    ];

    public function validate($data){
        $this->number=$data['number'];
        $this->type=$data['type'];
        $this->content=trim($data['content']);
        if(empty($data['number'])){
            $this->error="必填，且必须大于0";
            return false;
        }
        if(floor($data['number'])!=$data['number']){
            $this->error="请输入整数，且必须大于0";
            return false;
        }
        $number=floor($data['number']);
        if($number<=0){
            $this->error="且必须大于0";
            return false;
        }
        $this->number=$number;
        if($this->type==2){
            if($this->number>$this->balance){
                $this->error="扣除的余额不能大于".$this->balance;
                return false;
            }
        }
        if($this->type==3){
            if($this->number>$this->balance){
                $this->error="冻结的余额不能大于".$this->balance;
                return false;
            }
            if(empty($this->content) || strlen($this->content)<5){
                $this->error_content="请填写冻结余额说明,且不能小于5个字符";
                return false;
            }
        }
        if($this->type==4){
            if($this->number>$this->frozen_balance){
                $this->error="解除冻结的的余额不能大于冻结余额".$this->frozen_balance;
                return false;
            }
            if(empty($this->content) || strlen($this->content)<5){
                $this->error_content="请填写解除冻结余额说明,且不能小于5个字符";
                return false;
            }
        }
        return true;
    }
    //插入或扣除用户余额
    public function add(){
        $model = new Model();
        $model->startTrans();
        $flag=false;
        $total=$this->total_balance+$this->number;
        $balance=$this->balance+$this->number;
        if($this->type==1){
            //添加余额
            $res=$model->table(C('DB_PREFIX').'user_balance')->where("id=".$this->id)->save([
                'total_balance'=>$total,
                'balance'=>$balance,
                'updatedtime'=>time()
            ]);
        }else{
            $res=$model->table(C('DB_PREFIX').'user_balance')->where("id=".$this->id)->save([
                'total_balance'=>$this->total_balance,
                'balance'=>$this->balance-$this->number,
                'use_balance'=>$this->use_balance+$this->number,
                'updatedtime'=>time()
            ]);
        }
        if($res){
            //添加余额日志
            if($this->type==1){
                $model->table(C('DB_PREFIX').'user_balance_log')->add([
                    'user_id'=>$this->user_id,
                    'type'=>0,
                    'current_balance'=>$this->balance,
                    'balance'=>$this->number,
                    'total_balance'=>$balance,
                    'createdtime'=>time(),
                    'description'=>'管理员手动添加 用户余额：'.$this->number
                ]);
            }else{
                $model->table(C('DB_PREFIX').'user_balance_log')->add([
                    'user_id'=>$this->user_id,
                    'type'=>1,
                    'current_balance'=>$this->balance,
                    'balance'=>$this->number,
                    'total_balance'=>$this->balance-$this->number,
                    'createdtime'=>time(),
                    'description'=>'管理员手动扣除 用户余额：'.$this->number
                ]);
            }
            $flag=true;
        }
        if(!$flag){
            $model->rollback();
        }else{
            $model->commit();
        }
        return $flag;
    }

    //冻结或解除冻结操作
    public function frozen(){
        $model = new Model();
        $model->startTrans();
        $flag=false;
        $total=$this->total_balance+$this->number;
        $balance=$this->balance+$this->number;
        if($this->type==3){
            //冻结余额
            $res=$model->table(C('DB_PREFIX').'user_balance')->where("id=".$this->id)->save([
                'balance'=>$this->balance-$this->number,
                'frozen_balance'=>$this->frozen_balance+$this->number,
                'updatedtime'=>time()
            ]);
        }else{
            $res=$model->table(C('DB_PREFIX').'user_balance')->where("id=".$this->id)->save([
                'balance'=>$this->balance+$this->number,
                'frozen_balance'=>$this->frozen_balance-$this->number,
                'updatedtime'=>time()
            ]);
        }
        if($res){
            //添加余额日志
            if($this->type==3){
                $model->table(C('DB_PREFIX').'user_balance_log')->add([
                    'user_id'=>$this->user_id,
                    'type'=>1,
                    'current_balance'=>$this->balance,
                    'balance'=>$this->number,
                    'total_balance'=>$this->balance-$this->number,
                    'createdtime'=>time(),
                    'description'=>'管理员手动冻结余额：'.$this->number." 原因：".$this->content
                ]);
            }else{
                $model->table(C('DB_PREFIX').'user_balance_log')->add([
                    'user_id'=>$this->user_id,
                    'type'=>0,
                    'current_balance'=>$this->balance,
                    'balance'=>$this->number,
                    'total_balance'=>$this->balance+$this->number,
                    'createdtime'=>time(),
                    'description'=>'管理员手动解除冻结：'.$this->number." 原因：".$this->content
                ]);
            }
            $flag=true;
        }
        if(!$flag){
            $model->rollback();
        }else{
            $model->commit();
        }
        return $flag;
    }
}